package com.xiaobai.zhuangxiuguanjia.adapters;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.xiaobai.zhuangxiuguanjia.R;
import com.xiaobai.zhuangxiuguanjia.Utils.ViewHolder;

import java.util.List;
import java.util.Map;

/**
 * Created by Android Studio
 * Project: ZhuangXiuGuanJia
 * Author: Jack
 * Email: jackfangqi1314@gmail.com/jackfangqi@163.com
 * Date: 2015/6/18
 */
public class MyGridViewAdapter extends BaseAdapter {
    private Context context;
    private List<Map<String, Object>> list;
    private int clickTemp = -1;

    public MyGridViewAdapter(Context context, List<Map<String, Object>> list) {
        this.context = context;
        this.list = list;
    }

    public void setClickTemp(int clickTemp) {
        this.clickTemp = clickTemp;
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (list != null) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null)
            convertView = LayoutInflater.from(context).inflate(R.layout.app_market_layout, parent, false);

        if (clickTemp == position)
            convertView.setBackgroundResource(R.drawable.shape_grid_view_selected);
        else
            convertView.setBackgroundColor(Color.TRANSPARENT);

        ImageView appIcon = ViewHolder.getView(convertView, R.id.app_market_icon);
        TextView appName = ViewHolder.getView(convertView, R.id.app_market_name);

        Map<String, Object> map = list.get(position);
        appIcon.setImageDrawable((Drawable) map.get("icon"));
        appName.setText(map.get("label").toString());

        return convertView;
    }
}
